package com.cqut.forum.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.User;
import com.cqut.forum.entity.UserNews;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.service.UserNewsService;
import com.cqut.forum.service.UserService;

/**
 * 访问用户时获取该用户的动态、博客、图片action。注：只能获取公共的数据
 * @author yangxun
 */
@Controller
public class UserVisitAction {
	@Resource
	private PictureGroupAction pictureGroupAction;
	@Resource
	private UserService userService;
	@Resource
	private UserNewsAction userNewsAction;
	@Resource
	private UserNewsService userNewsService;
	@Resource
	private ArticleAction articleAction;
	@Resource
	private ModuleService moduleService;
	
	@SuppressWarnings("deprecation")
	@RequestMapping("/user/visit_user")
	@ResponseBody
	public Map<String, Object> userVisitInfo(HttpServletRequest request,int targetId){
		User user = userService.getById(targetId);
		User visitor = (User)request.getSession().getAttribute("user");
		if(null!=visitor&&!visitor.getUserId().equals(user.getUserId())){
			UserNews un = userNewsService.isVisited(visitor.getUserId(),user.getUserId());
			if(un==null){
				un = new UserNews();
				un.setAcceptUserId(user.getUserId());
				un.setUser(visitor);
				un.setIsSystemNews(false);
				un.setModuleId(moduleService.getModuleIndex("动态"));
				un.setNewsContents("");
				un.setNewsTime(new Date());
				un.setUserNewsState(true);
				userNewsService.addUserNews(un);
			}
		}
		List<Map<String, Object>> notices = userService.getUserInfo(user,0,5);
		List<Map<String, Object>> articles = articleAction.articleListInit(targetId, "");
		List<Map<String, Object>> pictures = pictureGroupAction.loadGroups(targetId);
		Map<String, Object> userInfo = new HashMap<String, Object>();
		userInfo.put("userName", user.getUserName());
		userInfo.put("userSex", user.getUserSex());
		userInfo.put("userScore", user.getUserScore());
		userInfo.put("userPhoto", user.getUserPhoto());
		userInfo.put("userHover", user.getUserSign()==null?"":user.getUserSign());
		userInfo.put("userEmail",user.getUserEmail());
		userInfo.put("userPlace", user.getUserPlace()==null?"":user.getUserPlace());
		userInfo.put("lastLoginTime", ((Date)user.getUserLastLoginTime()).toLocaleString());
		
		List<Map<String, Object>> visitors = userNewsAction.getModuleVisitors(user.getUserId(), "动态", 0, 9);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("noticeInfo", notices);
		result.put("articleInfo", articles);
		result.put("pictureInfo", pictures);
		result.put("userInfo", userInfo);
		result.put("visitors", visitors);
		
		return result;
	}
	@RequestMapping("/visit/load_dongtai")
	@ResponseBody
	public List<Map<String, Object>> loadDongtai(int targetId,int index,int size){
		User user = userService.getById(targetId);
		List<Map<String, Object>> notices = userService.getUserInfo(user,index,size);
		return notices;
	}
}
